package com.zc.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zc.edu.client.VodClient;
import com.zc.edu.entity.Video;
import com.zc.edu.mapper.VideoMapper;
import com.zc.edu.service.VideoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author cong
 * @since 2022-11-25
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

	@Autowired
	private VodClient vodClient;

	@Override
	public boolean removeVideo(String courseId) {
		QueryWrapper<Video> wrapper = new QueryWrapper<>();
		wrapper.eq("course_id", courseId);
		wrapper.select("video_source_id");
		List<Video> videoList = this.list(wrapper);
		List videoIdList = new ArrayList<String>();
		videoList.forEach(video -> {
			String videoSourceId = video.getVideoSourceId();
			if(!StringUtils.isEmpty(videoSourceId)) {
				videoIdList.add(videoSourceId);
			}
		});
		if(videoIdList.size() > 0) {
			vodClient.deleteVideoList(videoIdList);
		}

		boolean remove = this.remove(wrapper);
		return remove;
	}
}
